package com.cff.action.imggps;

import java.awt.Image;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Iterator;
import java.util.Set;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import com.cff.util.EventUtil;

public class GpsImgOpenActionListener extends GpsImgActionListener{
	private JFrame tmpFrame;
	private JLabel lastJLabel;
	private ImageIcon lastImageIcon;
	public JFrame getTmpFrame() {
		return tmpFrame;
	}

	public void setTmpFrame(JFrame tmpFrame) {
		this.tmpFrame = tmpFrame;
	}
	
	public GpsImgOpenActionListener(JFrame tmpFrame) {
		super();
		this.tmpFrame = tmpFrame;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(lastJLabel!=null){
			tmpFrame.remove(lastJLabel);
		}
		JFileChooser jfc=new JFileChooser(new File("D:/Users/Yuri/Pictures"));  
        jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );  
        jfc.showDialog(new JLabel(), "选择");
        
        File file=jfc.getSelectedFile();  
        if(file == null)return;
        if(file.isDirectory()){  
            System.out.println("文件夹:"+file.getAbsolutePath());  
            return;
        }else if(file.isFile()){  
            System.out.println("文件:"+file.getAbsolutePath());  				            
        }  
        ImageIcon icon = new ImageIcon(file.getAbsolutePath());
        setFilePath(file.getAbsolutePath());
        JLabel jlpic = new JLabel();
        int width = icon.getIconWidth();
        int height = icon.getIconHeight();
        if(width > tmpFrame.getWidth()/2-20 ){
        	float rate = width/(tmpFrame.getWidth()/2-20);
        	width = tmpFrame.getWidth()/2-20;
        	height = (int) (height/rate);
        }
        if(height > tmpFrame.getHeight()-50){
        	float rate = height/(tmpFrame.getHeight()-50);
        	height = tmpFrame.getHeight()-50;
        	width = (int) (width/rate);
        }
        icon.setImage(icon.getImage().getScaledInstance(width,
        		height, Image.SCALE_AREA_AVERAGING));
              System.out.println(icon.getIconHeight() + "" + icon.getIconWidth());
        System.out.println(width + "---" + height);
        jlpic.setBounds(10, 10, width , height);
        jlpic.setHorizontalAlignment(0);
        jlpic.setIcon(icon);
        tmpFrame.add(jlpic);
        tmpFrame.pack();
        lastJLabel = jlpic;
        setToDefault(tmpFrame);
        tmpFrame.setVisible(true);
	}

}
